home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3856 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  74 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: Fri, 26 Jan 1996 12:35:40 GMT
  6. Organization: Netcom
  7. Message-ID: <3108c867.40236096@nntp.ix.netcom.com>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <hamilton-1801962045570001@dialup-147.austin.io.com> <4dpcfo$293@clarknet.clark.net> <hamilton-2401960104020001@dialup-86.austin.io.com>
  9. NNTP-Posting-Host: ix-dc7-03.ix.netcom.com
  10. X-NETCOM-Date: Fri Jan 26  4:35:41 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. hamilton@shokwave.com (Jim Hamilton) wrote:
  14.  
  15. > In article <4dpcfo$293@clarknet.clark.net>, gusty@clark.net (Harlan
  16. > Messinger) wrote:
  17. > >Jim Hamilton (hamilton@shokwave.com) wrote:
  18. > >: In article <4dfhlu$a33$1@mhafn.production.compuserve.com>, Holger Maier
  19. > >: <100336.3326@CompuServe.COM> wrote:
  20. > >: 
  21. > >: >Consider
  22. > >: >#include <iostream>
  23. > >: >int main() {
  24. > >: >  int i=1;int j=i+(i+=1);
  25. > >: >  cout<<i<<','<<j<<'\n';
  26. > >: >  return 0;
  27. > >: >}
  28. > >: >on my compiler this produces 2,4
  29. > [deleted]
  30. >  
  31. > >: The highest precedence in any expression is the insides of parentheses
  32. > >: ().  Therefore (i+=1) is evaluated before i+().
  33. > >
  34. > >True, but that's not actually the problem here. The problem is whether 
  35. > >(i+=1) gets evaluated before the i to the left of the plus sign.
  36. > And I said, what's inside the parentheses (i+=1) *does* gets evaluated
  37. > before the i to the left of the plus sign.
  38.  
  39. You said it, but it's wrong.  The order of evaluation of operands is
  40. undefined.  In i + (i += 1), there are two operands of +, i and (i +=
  41. 1).  Either may be evaluated first.
  42.  
  43. Actually, this is theoretical since the draft, following the C
  44. standard, is much less forgiving (5)
  45.  
  46.     Except where noted, the order of evaluation of operands of
  47.     individual operators and subexpressions of individual
  48.     expressions, and the order in which side effects take place, 
  49.     is unspecified.  Between the  previous and next sequence  
  50.     point a scalar object shall have its stored value modified at 
  51.     most once by the evaluation of an expression.  Furthermore,  
  52.     the prior value shall be accessed only to determine the
  53.     value to be stored.  The requirements of this paragraph shall 
  54.     be met for each allowable ordering of the subexpressions of a 
  55.     full expression; otherwise the behavior is undefined.  
  56.     
  57.     [Example:
  58.           i = v[i++];      // the behavior is undefined
  59.           i = 7,i++,i++;   // `i' becomes 9
  60.  
  61.           i = ++i + 1;     // the behavior is undefined
  62.           i = i + 1;       // the value of 'i' is incremented
  63.  
  64. There is no sequence point in i + (i += 1) save at the end of the full
  65. expression, so i is both modified and it's value accessed other than
  66. to determine the value to be stored.  This results in undefined
  67. behavior and there are no requirements on the compilers actions -- it
  68. does not have to compute a value at all.
  69.  
  70.  
  71. Michael M Rubenstein
  72.